home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / graphics / sfwjpg / src / flipjpeg.c < prev    next >
C/C++ Source or Header  |  1999-06-15  |  7KB  |  252 lines

  1. /*
  2.  * flipjpeg.c
  3.  *
  4.  * Copyright (c) 1997  Everett Lipman
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License
  8.  * as published by the Free Software Foundation; either version 2
  9.  * of the License, or (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * The GNU General Public License is available at
  17.  *
  18.  *    http://www.fsf.org/copyleft/gpl.html
  19.  *
  20.  * Alternatively, you can write to the
  21.  *
  22.  *    Free Software Foundation, Inc.
  23.  *    59 Temple Place - Suite 330
  24.  *    Boston, MA  02111-1307, USA
  25.  *
  26.  * ---------------------------------------------------------------
  27.  *
  28.  * This program will do a lossless vertical inversion of a jpeg
  29.  * file.  It will only work properly if the jpeg image has a height
  30.  * in pixels which is a multiple of 8.
  31.  *
  32.  * This program requires the Independent JPEG Group's free JPEG software.
  33.  * You can get a copy at
  34.  *
  35.  *   ftp://ftp.uu.net/graphics/jpeg
  36.  *
  37.  * Most of the code here was taken from the program jpegtran.c, which
  38.  * is part of the IJG software package.  Since I have little experience
  39.  * using the IJG package, I am not sure if this program will run on any
  40.  * platform other than Sun Solaris (UNIX).
  41.  *
  42.  */
  43.  
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include "cdjpeg.h"       /* Common decls for cjpeg/djpeg applications */
  47. #include "jversion.h"     /* for version message */
  48.  
  49. #ifdef AMIGA /* ARK, 10/Jul/98 */
  50. #include "amiga.h"
  51. #endif
  52.  
  53. static const char * progname;  /* program name for error messages */
  54.  
  55. LOCAL(void)
  56. usage (void)
  57. /*
  58.  * complain about bad command line
  59.  */
  60. {
  61.   fprintf(stderr, "\nusage: flipjpeg < inputfile > outputfile\n\n");
  62.   exit(EXIT_FAILURE);
  63. }
  64.  
  65.  
  66. /*
  67.  * The main program.
  68.  */
  69. int
  70. main (int argc, char **argv)
  71. {
  72.   struct jpeg_decompress_struct srcinfo;
  73.   struct jpeg_compress_struct dstinfo;
  74.   struct jpeg_error_mgr jsrcerr, jdsterr;
  75.   jvirt_barray_ptr *coef_arrays;
  76.   JDIMENSION i, compnum, rownum, blocknum;
  77.   size_t block_row_size;
  78.   JBLOCKARRAY coef_buffers[MAX_COMPONENTS];
  79.   JBLOCKARRAY row_ptrs[MAX_COMPONENTS];
  80.   FILE * input_file;
  81.   FILE * output_file;
  82.  
  83.   if (argc != 1) usage();
  84.  
  85.   progname = argv[0];
  86.   if (progname == NULL || progname[0] == 0)
  87.     progname = "flipjpeg";  /* in case C library doesn't provide it */
  88.  
  89.   /*
  90.    * Initialize the JPEG decompression object with default error handling.
  91.    */
  92.  
  93.   srcinfo.err = jpeg_std_error(&jsrcerr);
  94.   jpeg_create_decompress(&srcinfo);
  95.  
  96.   /*
  97.    * Initialize the JPEG compression object with default error handling.
  98.    */
  99.  
  100.   dstinfo.err = jpeg_std_error(&jdsterr);
  101.   jpeg_create_compress(&dstinfo);
  102.  
  103.   jsrcerr.trace_level = jdsterr.trace_level;
  104.   srcinfo.mem->max_memory_to_use = dstinfo.mem->max_memory_to_use;
  105.  
  106.   /*
  107.    * Use stdin and stdout
  108.    */
  109.  
  110.   input_file = read_stdin();
  111.   output_file = write_stdout();
  112.  
  113.   /*
  114.    * Specify data source for decompression
  115.    */
  116.  
  117.   jpeg_stdio_src(&srcinfo, input_file);
  118.  
  119.   /*
  120.    * Read file header
  121.    */
  122.  
  123.   (void) jpeg_read_header(&srcinfo, TRUE);
  124.  
  125.   /*
  126.    * Allocate memory to hold entire coefficient array for all components
  127.    */
  128.  
  129.   for (compnum=0; compnum<srcinfo.num_components; compnum++)
  130.     coef_buffers[compnum] = ((&dstinfo)->mem->alloc_barray)
  131.                             ((j_common_ptr) &dstinfo, JPOOL_IMAGE,
  132.                              srcinfo.comp_info[compnum].width_in_blocks,
  133.                              srcinfo.comp_info[compnum].height_in_blocks);
  134.  
  135.   /*
  136.    * Read source file as DCT coefficients
  137.    */
  138.  
  139.   coef_arrays = jpeg_read_coefficients(&srcinfo);
  140.  
  141.   /*
  142.    * Initialize destination compression parameters from source values
  143.    */
  144.  
  145.   jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
  146.  
  147. /***************************************************************************/
  148.  
  149.   /*
  150.    * print out some info about the image
  151.    */
  152. /*
  153.  * fprintf(stderr,"\ncomponents: %d  image width: %d  image height: %d\n\n",
  154.  *         srcinfo.num_components, srcinfo.image_width, srcinfo.image_height);
  155.  *
  156.  * for (compnum=0; compnum < srcinfo.num_components; compnum++)
  157.  *   fprintf(stderr,
  158.  *           " component: %d  width in blocks: %d  height in blocks: %d\n",
  159.  *           compnum,srcinfo.comp_info[compnum].width_in_blocks,
  160.  *           srcinfo.comp_info[compnum].height_in_blocks);
  161.  * fprintf(stderr,"\n");
  162.  */
  163.  
  164. /***************************************************************************/
  165.  
  166.   /*
  167.    * Copy coefficients into new real array, flipping the sign
  168.    * of each coefficient corresponding to a DCT basis
  169.    * function which is asymmetric in the vertical direction.
  170.    */
  171.  
  172.   for (compnum=0; compnum<srcinfo.num_components; compnum++)
  173.     {
  174.     block_row_size = (size_t) SIZEOF(JCOEF)*DCTSIZE2
  175.                               *srcinfo.comp_info[compnum].width_in_blocks;
  176.     for (rownum=0; rownum<srcinfo.comp_info[compnum].height_in_blocks; rownum++)
  177.       {
  178.       row_ptrs[compnum] = ((&dstinfo)->mem->access_virt_barray)
  179.                           ((j_common_ptr) &dstinfo, coef_arrays[compnum],
  180.                             rownum, (JDIMENSION) 1, FALSE);
  181.       for (blocknum=0; blocknum<srcinfo.comp_info[compnum].width_in_blocks;
  182.            blocknum++)
  183.         for (i=0; i<DCTSIZE2; i++)
  184.           {
  185.           if ( (i/DCTSIZE)%2 == 1 )
  186.             {
  187.             coef_buffers[compnum][rownum][blocknum][i] =
  188.             -row_ptrs[compnum][0][blocknum][i];
  189.             }
  190.           else
  191.             {
  192.             coef_buffers[compnum][rownum][blocknum][i] =
  193.             row_ptrs[compnum][0][blocknum][i];
  194.             }
  195.           }
  196.       }
  197.     }
  198.  
  199. /***************************************************************************/
  200.  
  201.   /*
  202.    * Write coefficient block rows back into virtual array, in
  203.    * upside-down order.
  204.    */
  205.  
  206.   for (compnum=0; compnum<srcinfo.num_components; compnum++)
  207.     {
  208.     block_row_size = (size_t) SIZEOF(JCOEF)*DCTSIZE2
  209.                               *srcinfo.comp_info[compnum].width_in_blocks;
  210.     for (rownum=0; rownum<srcinfo.comp_info[compnum].height_in_blocks; rownum++)
  211.       {
  212.       row_ptrs[compnum] = ((&dstinfo)->mem->access_virt_barray)
  213.                           ((j_common_ptr) &dstinfo, coef_arrays[compnum],
  214.                            rownum, (JDIMENSION) 1, TRUE);
  215.       memcpy(row_ptrs[compnum][0][0],
  216.              coef_buffers[compnum]
  217.              [(srcinfo.comp_info[compnum].height_in_blocks-1)-rownum][0],
  218.              block_row_size);
  219.       }
  220.     }
  221.  
  222. /***************************************************************************/
  223.  
  224.   /*
  225.    * Specify data destination for compression
  226.    */
  227.  
  228.   jpeg_stdio_dest(&dstinfo, output_file);
  229.  
  230.   /*
  231.    * Start compressor
  232.    */
  233.  
  234.   jpeg_write_coefficients(&dstinfo, coef_arrays);
  235.  
  236.   /*
  237.    * Finish compression and release memory
  238.    */
  239.  
  240.   jpeg_finish_compress(&dstinfo);
  241.   jpeg_destroy_compress(&dstinfo);
  242.   (void) jpeg_finish_decompress(&srcinfo);
  243.   jpeg_destroy_decompress(&srcinfo);
  244.  
  245.   /*
  246.    * All done.
  247.    */
  248.  
  249.   exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
  250.   return 0;      /* suppress no-return-value warnings */
  251. }
  252.